package org.yxj.changethemeapp

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import com.youth.banner.BannerConfig
import com.youth.banner.Transformer
import com.youth.banner.loader.ImageLoaderInterface
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.item_home_layout.*
import org.yxj.changethemeapp.v.MyBanner
import org.yxj.changethemeapp.v.MyViewPager

class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        showViewPager()
    }

    private var mMyViewPager: MyViewPager? = null
    var v: View? = null
    private fun showViewPager() {
        val l = mutableListOf<View>()
        v = LayoutInflater.from(this).inflate(R.layout.item_home_layout, null) as View
        l.add(v!!)
        l.add(initItemView())
        l.add(initItemView())
        l.add(initItemView())
        l.add(initItemView())
        l.add(initItemView())
        mViewPager.adapter = MyPagerAdapter(l)

        val l1 = mutableListOf<View>()
        l1.add(initItemView())
        l1.add(initItemView())
        l1.add(initItemView())
        mMyViewPager = v?.findViewById(R.id.mMyViewPager)
        mMyViewPager?.adapter = MyPagerAdapter(l1)

        v?.let { showBanner(it) }
        showCurrentTheme()
    }


    private fun showCurrentTheme() {
        val btnChange = v?.findViewById(R.id.btnChange) as Button
        btnChange.text = if (CacheThemeManager.isDarkModeStatus(this)) "白色" else "深色"
    }

    fun aaaa(view: View) {
        if (CacheThemeManager.isDarkModeStatus(this)) {
            CacheThemeManager.save(this, false)
        } else {
            CacheThemeManager.save(this, true)
        }
        showCurrentTheme()
    }


    private fun initItemView(): View {
        val img = ImageView(this)
        img.setImageResource(R.mipmap.ic_launcher)
        return img
    }


    inner class MyPagerAdapter(var l: List<View>) : PagerAdapter() {

        override fun getCount(): Int {
            return l.size
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view == `object`
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = l[position]
            container.addView(view)
            return view
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(l[position])
        }
    }


    private fun showBanner(v: View) {
        val l1 = mutableListOf<String>()
        l1.add("")
        l1.add("")
        l1.add("")
        val mBanner = v.findViewById<MyBanner>(R.id.mBanner)
        mBanner.count = l1.size
        mBanner.apply {
            //设置banner样式
            //设置样式,默认为:Banner.NOT_INDICATOR(不显示指示器和标题)
            //1. Banner.CIRCLE_INDICATOR    显示圆形指示器
            //2. Banner.NUM_INDICATOR       显示数字指示器
            //3. Banner.NUM_INDICATOR_TITLE 显示数字指示器和标题
            //4. Banner.CIRCLE_INDICATOR_TITLE  显示圆形指示器和标题
            setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
            //设置轮播样式（没有标题默认为右边,有标题时默认左边）
            //可选样式:
            //Banner.LEFT   指示器居左
            //Banner.CENTER 指示器居中
            //Banner.RIGHT  指示器居右
            setIndicatorGravity(BannerConfig.CENTER)
            //设置图片加载器
            setImageLoader(GlideImageLoader())
            //设置图片集合
            setImages(l1)
            //设置banner动画效果
            setBannerAnimation(Transformer.Default)
            //设置标题集合（当banner样式有显示title时）
//            setBannerTitles(l1)
            //设置自动轮播，默认为true
            isAutoPlay(true)
            //设置轮播时间
            setDelayTime(3000)
            //banner设置方法全部调用完毕时最后调用
            start()
        }
    }
}


class GlideImageLoader : ImageLoaderInterface<View> {
    override fun displayImage(context: Context?, path: Any?, imageView: View?) {
    }

    override fun createImageView(context: Context?): View {
        val im = ImageView(context)
        im.setImageResource(R.mipmap.ic_launcher)
        return im;
    }
}
